package com.cnxdc.demo.controller;

import com.cnxdc.demo.service.CategoryService;
import com.cnxdc.demo.vo.Category;
import com.cnxdc.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.add(category);
        return Result.success();
    }
    @GetMapping
    public Result<List<Category>> getall(){
        return Result.success(categoryService.getall());
    }
    @GetMapping("/detail")
    public Result<Category> detail(Integer id) {
        return Result.success(categoryService.detail(id));
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(Integer id) {
        //查询一下 这个分类下是否有文章：该类别下有文章，不能删除
        categoryService.delete(id);
        return Result.success();
    }
}
